<template>
  <view class="login-container">
    <!-- 提示登录的图标 -->
    <uni-icons type="contact-filled" size="100" color="#AFAFAF"></uni-icons>
    <!-- 登录按钮 -->
    <button type="primary" class="btn-login" @click="getUserProfile">一键登录</button>
    <!-- 登录提示 -->
    <view class="login-tips">
      登录后尽享更多权益
    </view>
  </view>
</template>

<script>
  import { mapMutations , mapState} from 'vuex'
  export default {
    name:"my-login",
    data() {
      return {
        
      };
    },
    
    computed: {
      ...mapState('m_user', ['redirectInfo'])
    },
    
    methods: {
      ...mapMutations('m_user', ['saveUserInfo','updateToken']),
      ...mapMutations('m_user', ['updateRedirectInfo']),
      // 调用微信 api 获取微信用户信息
      async getUserProfile () {
        const [err, succ] = await uni.getUserProfile({desc: '完善个人资料' }).catch(err => err)
        // 异常数据判断
        if (err) return uni.$showMsg('您拒绝了授权！')
        // 获取到用户信息, 保存到 vuex
        console.log(succ)
        this.saveUserInfo(succ.userInfo)
        
        // 获取token (登录黑马优购接口)
        this.getToken(succ)
      },
      
      // 获取 token 的方法
     async getToken(info) {
        // 1- 微信登录 (code)
        const [err, res] = await uni.login().catch(err => err)
        // 异常数据判断
        if (err) return uni.$showMsg('微信登录失败！')
        
        console.log(res)
        // 2- 黑马优购登录
        // 准备登录接口数据
        const data = {
          encryptedData: info.encryptedData,
          rawData: info.rawData,
          iv: info.iv,
          signature: info.signature,
          code: res.code,
        }
        // 调用登录接口
        const result = await uni.$http.post('/users/wxlogin', data)
        console.log(result)
        
        // 数据异常判断
        // if (result.meta.status !== 200) return uni.$showMsg('黑马优购登录失败')
        
        // 保存token 到 vuex 中
        uni.$showMsg('黑马优购登录成功')
        this.updateToken('Bearer eyJhbGci01JIUzINiTsInRScCI6IkpXVC9.eyJlaNO10fU1LJpYXQ10jE2MzQ30DMЗ0TksImVdcCI6MTANYzNDc4Иzc5OН e .alLB35GMvfXVdigLrjojntiБ ywGhkyA -1_u4IOЗЗ')
        
        // 返回购物车页面
        if ( this.redirectInfo && this.redirectInfo.openType == 'switchTab' && this.redirectInfo.from) {
          uni.switchTab({
            url: this.redirectInfo.from,
            success: () => {
              // 清空 redirectInfo
              this.updateRedirectInfo(null)
            }
          })
        }
      }
    }
  }
</script>

<style lang="scss"> 
.login-container {
  height: 750rpx;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background-color: #f8f8f8;
  position: relative;
  overflow: hidden;
  &::after {
    content: '';
    display: block;
    position: absolute;
    width: 100%;
    height: 80rpx;
    left: 0;
    bottom: 0;
    background-color: white;
    border-radius: 100%;
    transform: translateY(50%);
  }
  .btn-login {
    width: 90%;
    border-radius: 200rpx;
    margin: 30rpx 0;
    background-color: #c00000;
  }
  .login-tips {
    font-size: 24rpx;
    color: gray;
  }
}
</style>
